package com.baike.controller;

import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbTeacher;
import com.baike.mapper.TbTeacherMapper;
import com.baike.service.ITbTeacherService;
import com.baike.util.EmailUtil;
import com.baike.util.EmailUtil2;
import com.baike.util.ShiroUtils;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author:pjm
 */
@RestController
@RequestMapping("loginController")
@Api(value = "教师登录管理")
@RefreshScope //开启自动刷新配置
public class LoginController {
    @Autowired
    private TbTeacherMapper tbTeacherMapper;
    @Autowired
    private ITbTeacherService tbTeacherService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private EmailUtil2 emailUtil2;

    //教师登录
    @ApiOperation(value = "教师登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherName",value = "教师账户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "teacherPassword",value = "密码",required = true,paramType = "query")
    })
    @GetMapping
    public Result login(@RequestParam(value = "teacherName")String teacherName,
                        @RequestParam(value = "teacherPassword")String teacherPassword){
        //使用shiro登录功能
        //1,需要获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //将账号密码封装成token对象,参数要传过去
        UsernamePasswordToken token = new UsernamePasswordToken(teacherName,teacherPassword);
        //调用登录验证,login的登录逻辑方法
        try {
            subject.login(token);
        }catch (UnknownAccountException un){
            return Result.build(ConstantEnum.UNKNOWN).setResult("账号不存在");
        }catch (IncorrectCredentialsException in){
            return Result.build(ConstantEnum.INCORRECT).setResult("密码错误");
        }
        return Result.build(ConstantEnum.SUCCESS).setResult("登录成功");

    }

    //忘记密码,密码重置
    @ApiOperation(value = "接收验证码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherName",value = "教师名称",required = true,paramType = "query")
    })
    @GetMapping(value = "/toCode")
    public Result toCode(@RequestParam(value = "teacherName")String teacherName) throws Exception {
        TbTeacher teacher = tbTeacherMapper.selectByUserName(teacherName);
        //发送邮箱验证码
        String email = teacher.getEmail();
        String s = emailUtil.toCode(email);
        //账号名称作为key存入redis
        redisTemplate.boundHashOps("codes").put(teacherName,s);
        return Result.build(ConstantEnum.SUCCESS);

    }

    //拿到邮箱验证码,进行修改账号密码
    @ApiOperation(value = "密码重置接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "邮箱验证码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "teacherName",value = "教师名称",required = true,paramType = "query"),
    })
    @PutMapping
    public Result updatePassword(@RequestParam(value = "code")String code,
                                 @RequestParam(value = "teacherName")String teacherName) throws Exception {
        TbTeacher teacher = tbTeacherMapper.selectByUserName(teacherName);
        //发送密码
        String email = teacher.getEmail();
        //取出redis中的key
        String vcode = (String) redisTemplate.boundHashOps("codes").get(teacherName);
        //判断输入的验证码是否一致
        if(!vcode.equals(code)){
            return Result.build(ConstantEnum.ERROR) ;
        }
        //重置密码并加盐
        String teacherPassword = "123";
        emailUtil2.toCode(email,teacherPassword);
        //设置加盐长度
        String salt = ShiroUtils.generateSalt(6);
        String pwd = ShiroUtils.encryptPassword("MD5", teacherPassword, salt, 3);
        //修改用户密码
        tbTeacherService.toUpdatePassword(teacherName,pwd,salt);
        redisTemplate.delete("codes");
        return Result.build(ConstantEnum.UPDATE_SUCCESS);

    }

}
